package com.whzjkj.gateway.server.adapter;

import com.whzjkj.gateway.server.processor.TerminalMessageProcessor;
import com.whzjkj.gateway.vo.PackageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description: 指令适配器
 * @author: Mr.Hu
 * @create: 2018-11-01 15:09
 **/
@Component
public class TerminalMessageProcessorAdapter {

    private List<TerminalMessageProcessor> terminalMessageProcessors;

    private TerminalMessageProcessor terminalMessageProcessor;

    @Autowired(required = false)
    public void setTerminalMessageProcessors(List<TerminalMessageProcessor> terminalMessageProcessors) {
        this.terminalMessageProcessors = terminalMessageProcessors;
    }

    public void processTerminalMessage(PackageData data) throws Exception {
        support(data);
        if (terminalMessageProcessor!=null){
            terminalMessageProcessor.processTerminalMessage(data);
        }
    }

    public void support(PackageData data) {
        terminalMessageProcessor = null;
        terminalMessageProcessors.forEach(terminalMessageProcessor->{
            if (terminalMessageProcessor.support(data)){
               setTerminalMessageProcessor(terminalMessageProcessor);
            }
        });
    }

    public void setTerminalMessageProcessor(TerminalMessageProcessor terminalMessageProcessor) {
        this.terminalMessageProcessor = terminalMessageProcessor;
    }
}
